
<template>
  <el-dialog v-model="dialogFormVisible" class="dialog-title" :title="title" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="margin-left:50px;">
      <el-form-item label="分组名称" prop="name">
        <el-input v-model="temp.name" />
      </el-form-item>
      <el-form-item label="分组编码" prop="code">
        <el-input v-model="temp.code" />
      </el-form-item>
      <el-form-item label="分组备注">
        <el-input v-model="temp.remarks" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="Please input" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogFormVisible = false">取消</el-button>
      <el-button v-permission="['sys:dict:group:update']" type="primary" :loading="loading" @click="title==='新增'?createData():updateData()">
        确定
      </el-button>
    </template>
  </el-dialog>
</template>

<script>

import { createDictGroup, updateDictGroup } from '@/api/sys/dict/dictGroup'

export default {
  name: 'DictGroupForm',
  emits: ['refreshList'],
  data() {
    return {
      dialogFormVisible: false,
      temp: {
        id: undefined,
        name: '',
        code: '',
        remarks: ''
      },
      loading: false,
      title: undefined,
      rules: {
        name: [{ required: true, message: '分组名称必填' }],
        code: [{ required: true, message: '分组编码必填' }]
      }
    }
  },

  methods: {
    /**
     * 重置表单
     */
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
        name: '',
        code: '',
        remarks: ''
      }
    },
    /**
     * 获取字典列表
     */
    refreshList() {
      console.log(4455)
      this.$emit('refreshList')
    },
    /**
     *提交新增
     */
    createData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          createDictGroup(this.temp).then((response) => {
            this.loading = false
            const data = response.data
            if (data.success) {
              this.$store.dictStore.initDict()
              this.dialogFormVisible = false
              this.$message.success('创建成功')
              this.refreshList()
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    /**
     * 编辑
     * @param row 选中数据
     */
    updateData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          const tempData = Object.assign({}, this.temp)
          updateDictGroup(tempData).then((response) => {
            this.loading = false
            const data = response.data
            if (data.success) {
              this.$store.dictStore.initDict()
              this.dialogFormVisible = false
              this.$message.success(data.msg)
              this.refreshList()
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    /**
     * 提交编辑
     * @param row 选中数据
     */
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.title = '编辑'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    },
    /**
     * 提交新增

     */
    handleCreate() {
      this.resetTemp()
      this.title = '新增'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate()
      })
    }
  }
}
</script>
<style scoped lang="scss">

</style>
